package cn.zj.logistics.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.zj.logistics.pojo.BaseData;
import cn.zj.logistics.pojo.Customer;
import cn.zj.logistics.pojo.CustomerExample;
import cn.zj.logistics.pojo.MessageObject;
import cn.zj.logistics.pojo.Order;
import cn.zj.logistics.pojo.OrderDetail;
import cn.zj.logistics.pojo.OrderExample;
import cn.zj.logistics.pojo.OrderExample.Criteria;
import cn.zj.logistics.pojo.User;
import cn.zj.logistics.pojo.UserExample;
import cn.zj.logistics.service.BaseDataService;
import cn.zj.logistics.service.CustomerService;
import cn.zj.logistics.service.OrderService;
import cn.zj.logistics.service.UserService;
import cn.zj.logistics.util.Constant;

@Controller
@RequestMapping("/order")
public class OrderController {

	@Autowired
	private OrderService orderService;
	
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private CustomerService customerService;
	
	@Autowired
	private BaseDataService baseDataService;
	

	@RequiresPermissions("order:orderPage")
	@RequestMapping("/orderPage")
	public String orderPage() {

		return "orderPage";
	}
	
	/*
	 * 根据订单的id查询出订单明细表中对应的订单明细信息
	 * 
	 */
	
	@ResponseBody
	@RequestMapping("/detail")
	public List<OrderDetail> detail(Long orderId){
		
		List<OrderDetail> details = orderService.selectDetailsByOrderId(orderId);
		
		
		return details;
	}
	
	
	
	@RequiresPermissions("order:list")
	@RequestMapping("/list")
	@ResponseBody
	public <T> PageInfo<Order> list(@RequestParam(defaultValue = "1") int pageNum,
			@RequestParam(defaultValue = "10") int pageSize, String keyword) {

		PageHelper.startPage(pageNum, pageSize);

		OrderExample example = new OrderExample();

		// 前台传递关键字参数不为空的时候才参与条件查询
		if (StringUtils.isNotBlank(keyword)) {
			Criteria criteria1 = example.createCriteria();
			criteria1.andShippingNameLike("%" + keyword + "%");
		}

		List<Order> orders = orderService.selectByExample(example);
		
		PageInfo<Order> pageInfo = new PageInfo<>(orders);

		return pageInfo;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public MessageObject delete(Long orderId) {
		
	
		int row = orderService.deleteByPrimaryKey(orderId);

		MessageObject mo = MessageObject.createMo(0, "删除数据失败，请联系技术人员");

		if (row == 1) {
			mo = MessageObject.createMo(1, "删除数据成功");
		}
		return mo;
	}

	// 新增功能
	@RequestMapping("/insert")
	@ResponseBody
	public MessageObject insert(@RequestBody Order order) {
		System.out.println(order);
		System.out.println(order.getOrderDetails());
		int row = orderService.insert(order);

		MessageObject mo = MessageObject.createMo(0, "新增数据数据失败，请联系技术人员");

		if (row == 1) {
			mo = MessageObject.createMo(1, "新增数据成功");
		}
		return mo;
	}

	// 新增功能
	@RequestMapping("/update")
	@ResponseBody
	public MessageObject update(Order order) {

		int row = orderService.updateByPrimaryKeySelective(order);

		MessageObject mo = MessageObject.createMo(0, "修改数据数据失败，请联系技术人员");

		if (row == 1) {
			mo = MessageObject.createMo(1, "修改数据成功");
		}
		return mo;
	}

	@RequestMapping("/orderEdit")
	public String orderEdit(Model m, Long orderId) {
		
		
		/*
		 * 查询出回显数据，为新增修改订单提供数据
		 * 
		 * 1，业务员（管理员表）
		 * 2，客户（客户表）
		 * 3，付款方式（基础数据表）
		 * 4，货运方式（基础数据表）
		 * 5，区间管理（基础数据表）
		 * 6，取件方式（基础数据表）
		 * 7，单位（基础数据表）
		 */
		
		
		
		// 1，业务员（管理员表）
		UserExample example = new UserExample();
		cn.zj.logistics.pojo.UserExample.Criteria criteria = example.createCriteria();
		criteria.andRolenameEqualTo(Constant.ROLE_SALESMAN);
		List<User> users = userService.selectByExample(example);
		m.addAttribute("users", users);
		// 2，客户（客户表）
		CustomerExample customerExample = new CustomerExample();
		List<Customer> customers = customerService.selectByExample(customerExample);
		m.addAttribute("customers", customers);
		
		 // 3，付款方式（基础数据表）
		List<BaseData> payments = baseDataService.selectBaseDatasByParentName(Constant.BASIC_PAYMENT_TYPE);
		m.addAttribute("payments", payments);
		
		 // 4，货运方式（基础数据表）
		List<BaseData> freights = baseDataService.selectBaseDatasByParentName(Constant.BASIC_FREIGHT_TYPE);
		m.addAttribute("freights", freights);	
		 // 5，区间管理（基础数据表）
		List<BaseData> intervals = baseDataService.selectBaseDatasByParentName(Constant.BASIC_COMMON_INTERVAL);
		m.addAttribute("intervals", intervals);	
		 // 6，取件方式（基础数据表）
		List<BaseData> fetchTypes = baseDataService.selectBaseDatasByParentName(Constant.BASIC_FETCH_TYPE);
		m.addAttribute("fetchTypes", fetchTypes);	
		 // 7，单位（基础数据表）
		List<BaseData> units = baseDataService.selectBaseDatasByParentName(Constant.BASIC_UNIT);
		m.addAttribute("units", units);	
		


		if (orderId != null) {
			// 查询出当前要修改的用户信息共享给编辑页面以供修改数据回显
			Order order = orderService.selectByPrimaryKey(orderId);
			m.addAttribute("order", order);
		}
		

		return "orderEdit";
	}



}
